package yawning.mew.cat;

import yawning.mew.ProcEffect.Trigger;
import yawning.mew.cat.CatModelSimState.CatTargetData;
import yawning.mew.character.CharacterModel.AttackResult;
import yawning.mew.sim.SimulationAction;
import yawning.mew.sim.SimulationTarget;

public class ClawAction implements SimulationAction {
	CatModelSimState mState;
	private SimulationTarget mTarget;
	private String mTargetName;
	private CatTargetData mTargetData;

	ClawAction(CatModelSimState state, CatTargetData targetData) {
		mState = state;
		mTarget = targetData.getSimulationTarget();
		mTargetData = targetData;
		mTargetName = mTarget.getTarget().getName();
	}

	public double perform() {
		int cost = mState.getEnergyCost(mState.mModel.clawCost);
		if (mState.getEnergy() < cost && !mState.mOocEv.isUp()) return SimulationAction.ACTION_FAIL;
		int prevEnergy = mState.getEnergy(); int prevCps = mState.getComboPoints();

		AttackResult result = (mTargetData.mCanParry) ? mState.mModel.getFrontalYellowResult() : mState.mModel.getYellowResult();

		if (result == AttackResult.MISS) {
			cost = (int) Math.round(cost * 0.2);
			mNrClawMiss++;

			if (mState.mIsLogging) mState.mLog.log("Claw: %s MISS\n", mTargetName);
		} else if (result == AttackResult.DODGE) {
			cost = (int) Math.round(cost * 0.2);
			mNrClawDodge++;

			if (mState.mIsLogging) mState.mLog.log("Claw: %s DODGE\n", mTargetName);
		} else if (result == AttackResult.PARRY) {
			cost = (int) Math.round(cost * 0.2);
			mNrClawParry++;

			if (mState.mIsLogging) mState.mLog.log("Claw: %s PARRY\n", mTargetName);
		} else {
			double damage = mState.mModel.getWeaponDamage() * mState.mModel.levelDep.getClawMult() + mState.mModel.levelDep.getClawDmg();

			if (result == AttackResult.CRIT) {
				mNrClawCrit++;
				damage *= mState.mModel.multCrit;
			} else {
				mNrClawHit++;
			}

			damage *= mState.getDirectDamageMult();
			mClawDamage += Math.round(damage);

			if (mState.mIsLogging) mState.mLog.log("Claw: %s %s Damage: %d\n", mTargetName, result, Math.round(damage));

			if (result == AttackResult.CRIT) mState.mSimEngine.triggerProc(mTarget, Trigger.YELLOWCRIT); else mState.mSimEngine.triggerProc(mTarget, Trigger.YELLOW);

			mState.onCpGenerator(result == AttackResult.CRIT);
		}

		if (mState.mOocEv.isUp()) mNrClawOoC++;
		if (mState.mTigersFuryEv.isUp()) mNrClawTF++;

		mClawEnergy += mState.onOoCConsumer(cost);

		mState.logResourceChange(prevEnergy, prevCps);

		return 1.0;
	}

	long mClawDamage;
	int mNrClawMiss;
	int mNrClawDodge;
	int mNrClawParry;
	int mNrClawCrit;
	int mNrClawHit;

	int mClawEnergy;
	int mNrClawOoC;
	int mNrClawTF;
};